<?php

require_once './include/common.inc.php';
checkLogin();

$page_title = 'jans2en furniture - Products - Product Details';
$menu_id = 5;
getMyParam('pid');
getMyParam('gotype');
getMyParam('new');
getMyParam('curpage');
getMyParam('categoryid');
getMyParam('settingid');
getMyParam('collectionid');
getMyParam('styleid');

$new_url = '';
if ($new == 1) {
    $menu_id = 7;
    $new_url = '&new=1'; 
}
//$fid = (int)$fid;
//
//$cate_id = $fid>0 ? $fid : $categoryid;

//if (!is_numeric($categoryid) || $categoryid <=0 ) {
//    header("Location:products.php");
//}


//// вƷid,ûвƷ
//$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p WHERE  not exists(select id from category where id=c.fid and publish = 0) AND  c.`id`=p.`category_id` and c.publish=1  GROUP BY c.`fid`";
//$query = $db->query($sql);
//while($rs = $db->fetch_array($query)) {
//    $fid_ary[] = $rs['fid'];   //ͼƬ
//}
//
//// sqlfid=0ʱεһЩֻиĲƷֻʣһ
//// ˵вƷ`hidden_flag` = 0ĲƷ
//$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p, `product` pr WHERE  not exists(select id from category where id=c.fid and publish = 0) AND pr.`id`=p.`product_id` AND c.`id`=p.`category_id` AND c.`fid` = 0    and c.publish=1 AND pr.`hidden_flag` = 0  GROUP BY p.`category_id`";
////$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p WHERE c.`id`=p.`category_id` AND c.`fid` = 0 GROUP BY p.`category_id`";
//$query = $db->query($sql);
//while($rs = $db->fetch_array($query)) {
//	$fid2_ary[] = $rs['category_id'];   //ͼƬ
//}
//
//
//foreach ($out_ary as $k=>$v) {
//    if (!in_array($k, $fid_ary) && !in_array($k, $fid2_ary)) {
//        unset($out_ary[$k]);
//    }
//}


//if ($HAS_NEW_ITEM) {
//    $out_ary[] = array(
//    'id' => '1001',
//    'name' => 'New Items',
//    'pic' => 'new_items.jpg',
//    'seq' => '1001',
//    'description' => 'Product development is a continuous process in our company'
//    );
//}
///*$out_ary[] = array(
//'id' => 1002,
//'name' => 'Specials',
//'pic' => 'specials.jpg',
//'seq' => '1002',
//'description' => 'Jans2en furniture has recently placed the following items on sale. A limited amount of inventory is available on a first come, first served basis. Our sales are a result of discontinued inventory, over stocked items, or occasionally we may just want to su'
//);*/
//
//$sub_out_ary = array();
//if ($cate_id > 0) {
//    //$sql = "SELECT `id`, `name`, `pic`, `seq`, `description` FROM `category` WHERE `fid` = $cate_id ORDER BY `seq` ASC";
//    $sql = "SELECT c.`id`, c.`name`, c.`pic`, c.`pic_2`, c.`seq`, c.`description` FROM `category` c, `product_category` pc WHERE  not exists(select id from category where id=c.fid and publish = 0) AND c.id=pc.`category_id` AND c.`fid` = $cate_id and c.publish=1 GROUP BY c.`id` ORDER BY c.`seq` ASC";
//    //echo $sql;
//    $query = $db->query($sql);
//    while($rs = $db->fetch_array($query)) {
//        $sub_out_ary[] = $rs;
//        $id_2_ary[] = $rs['id'];
//        if ($categoryid == $rs['id']) {
//            $category_name = trim($rs['name']);
//        }
//    }
//}
//
//
//if (!is_numeric($pid) || $pid <= 0) {
//    header("Location:product.php");
//    exit;
//}
//
//
//
//
//$no_preview_next = false;
//$current_page = 0;
////if ($categoryid == "" || $categoryid == 0 || $categoryid > 1000) {
//if ($categoryid == "" || $categoryid == 0 ) {
//    $no_preview_next = true;
//} else {
//    if ($gotype == "preview" || $gotype == "next") {
//        $pid_ary = array();
//        $thisid = false;
//        $new_pid = '';
//
//        // new_item
//        if ($cate_id>1000) {
//            $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag` FROM `product` p, `product_category` pc, `category` c WHERE  not exists(select id from category where id=c.fid and publish = 0) AND  c.`id`=pc.`category_id` AND pc.`product_id`=p.`id` AND p.`new_flag`=1 AND p.`hidden_flag`=0 and c.publish=1 GROUP BY p.`id` ORDER BY c.fid ASC, c.seq ASC, pc.seq desc, p.`code` DESC";
//            /*$query = $db->query($sql);
//            while($rs = $db->fetch_array($query)) {
//            $photo_ary[] = array('id'=>$rs['id'], 'name'=>$rs['name'], 'pic'=>$rs['pic'], 'code'=>$rs['code']);
//            }*/
//        } else {
//            if ($fid <= 0 && !empty($id_2_ary)) {
//                $id_2_str = implode(",", $id_2_ary);
//                $sql_3 = "AND c.`category_id` IN (" . $id_2_str . ")";
//            } else {
//                $sql_3 = "AND c.`category_id`='$categoryid'";
//            }
//            $sql = "SELECT p.`id` FROM `product` p, `product_category` c, `category` ca WHERE  not exists(select id from category where id=ca.fid and publish = 0) AND  ca.`id` = c.`category_id` AND p.`id`=c.`product_id` $sql_3 AND p.`hidden_flag`=0  and ca.publish=1 GROUP BY p.`id` ORDER BY ca.seq asc, c.seq desc, p.`code` DESC, p.`id` DESC";
//        }
//        //echo $sql;
//        //exit;
//
//        $query = $db->query($sql);
//        $index_num = 0;
//        while($rs = $db->fetch_array($query)) {
//            if ($new_pid == '') {
//                //echo $rs['id'];echo " | " . $pid;echo "<br>";
//                if ($gotype == "next" && $thisid) {
//                    $new_pid = $rs['id'];
//                }
//                if ($rs['id'] == $pid) {
//                    $thisid = true;
//                    if ($gotype == "preview") {
//                        $new_pid = empty($pid_ary) ? $pid : array_pop($pid_ary);
//                        $index_num--;
//                    }
//                }
//                $pid_ary[] = $rs['id'];
//                //echo $new_pid;
//                //echo "<pre>";print_r($pid_ary);//exit;
//                $index_num++;
//            }
//            
//        }
//
//        $current_page = ceil($index_num/12);
//        //echo $current_page;
//        if ($new_pid == "") {
//            $new_pid = $pid;
//        }
//        $pid = $new_pid;
//
//
//        //echo $pid;exit;
//    }
//}



// ¼ûʵҳϢ
// ͨһ $fid=0,$categoryid=һid
// ͨ $fid=һid,$categoryid=id
if ($fid==0 || $fid==$categoryid) {
    browseTracking(3,$pid,$categoryid);
} else {
    browseTracking(3,$pid,$fid . '||' . $categoryid);
}

//===========================check if the category is published=====//
$showProductCategory = true;  //the category of the product
if($fid >0 && $categoryid <=1000 )
{
	$sql = "select publish,(select publish from category where id = $fid) as f_publish from category where id = $categoryid";
	$publish_ary = $db->get_one($sql);
	if($publish_ary["publish"] == 0 || $publish_ary["f_publish"] == 0)
		$showProductCategory = false;
}
//=================================================================//


//еNext,Previewť--ʼ
if($gotype == "preview" || $gotype == "next")
{ 
	$pid_ary = array();
	$thisid = false;
	$new_pid = '';
	
	if($styleid >= 0 && $styleid !="")
	{
		$sql_style = " AND p.`style_id`= ".$styleid;		
		if($styleid == 0) $sql_style = "AND p.`style_id`>0";
		
		$sqlWhere = " WHERE c.publish=1 AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0 ".$sql_style." GROUP BY p.`id`";
		$sqlOrderBy = " ORDER BY p.`code` DESC ";
	}
	else if($settingid !=""  && $categoryid >= 0)
	{
		$cids = GetCategoryTreeIDS($_categories,$categoryid);
		if($settingid == 0 || $settingid == "") $strSettingSql = " AND p.`setting_id` > 0";
		else $strSettingSql = " AND p.`setting_id`= ".$settingid;
		
		$sqlWhere = " WHERE c.publish=1 AND c.`id` IN(".$cids.") AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0 ".$strSettingSql." GROUP BY p.`id`";
		$sqlOrderBy = " ORDER BY p.`code` DESC ";
	}
	else if($collectionid >= 0 && $collectionid!="")
	{
		$sql_collection = " AND p.`collection_id`= ".$collectionid;		
		if($collectionid == 0) $sql_collection = " AND p.`collection_id`>0";
		
		$sqlWhere = " WHERE c.publish=1 AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0 ".$sql_collection." GROUP BY p.`id`";
		$sqlOrderBy = " ORDER BY p.`code` DESC ";
	}
	else if($categoryid > 10000 && $settingid == "") //New Product
	{
		$sqlWhere = " WHERE c.publish=1 AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0  AND p.`new_flag`=1 GROUP BY p.`id`";
		$sqlOrderBy = " ORDER BY  p.`code` DESC ";
	}
	else if($categoryid >= 0 && $categoryid <=10000 && $settingid == "")
	{						
		$cids = GetCategoryTreeIDS($_categories,$categoryid);
		$sqlWhere = " WHERE c.publish=1 AND c.`id` IN(".$cids.") AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0  GROUP BY p.`id`";
		$sqlOrderBy = " ORDER BY p.`code` DESC ";
	}
	else
	{
		if($categoryid=="" ||$categoryid==0)
		{
			$sql = "SELECT c.* FROM `product_category` pc,`category` c WHERE c.id=pc.`category_id` AND pc.`product_id`='$pid' LIMIT 0,1";	
			$cate_info_ary = $db->get_one($sql);
			$categoryid = $cate_info_ary["id"];
			
			$cids = GetCategoryTreeIDS($_categories,$categoryid);
			$sqlWhere = " WHERE c.publish=1 AND c.`id` IN(".$cids.") AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0  GROUP BY p.`id`";
			$sqlOrderBy = " ORDER BY p.`code` DESC ";
		}
	}
	
	/*if($categoryid > 10001)
	{
		$sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag` FROM `product` p, `product_category` pc, `category` c WHERE  c.`id`=pc.`category_id` AND pc.`product_id`=p.`id` AND p.`new_flag`=1 AND p.`hidden_flag`=0 and c.publish=1 GROUP BY p.`id` ORDER BY c.fid ASC, c.seq ASC, pc.seq desc, p.`code` DESC";		
	}
	else
	{ 
		$sql = "SELECT p.`id` FROM `product` p, `product_category` c, `category` ca WHERE  ca.`id` = c.`category_id` AND p.`id`=c.`product_id` AND  c.`category_id` IN(".$cids.") AND p.`hidden_flag`=0  and ca.publish=1 GROUP BY p.`id` ORDER BY ca.seq asc, c.seq desc, p.`code` DESC, p.`id` DESC";
	}*/
	$sql = "SELECT p.`id` FROM `product` p, `product_category` pc, `category` c".$sqlWhere.$sqlOrderBy;
	$query = $db->query($sql);
	$index_num = 0;
	while($rs = $db->fetch_array($query)) {
		if ($new_pid == '') {
			if ($gotype == "next" && $thisid) {
				$new_pid = $rs['id'];
			}
			if ($rs['id'] == $pid) {
				$thisid = true;
				if ($gotype == "preview") {
					$new_pid = empty($pid_ary) ? $pid : array_pop($pid_ary);
					$index_num--;
				}
			}
			$pid_ary[] = $rs['id'];
			$index_num++;
		}
	}
	if($new_pid != '')
	{
		$pid = $new_pid;	
	}
	$curpage = ceil($index_num/12);
}
//еNext,Previewť--


//=============================== ƷϢʼ ==================================//
$sql = "SELECT * FROM `product` WHERE `id`='$pid'";
$product_info_ary = $db->get_one($sql);

$clearance_flag = false;

$item_flag = '';
$product_info_ary['code'] = strtoupper($product_info_ary['code']);
if ($product_info_ary['new_flag'] == 1 && $clearance_flag) {
    $item_flag = "<font color='red' size='2'><em>新产品! Clearance!</em></font>";
} elseif ($product_info_ary['new_flag'] == 1) {
    $item_flag = "<font color='red' size='2'><em>新产品!</em></font>";
} elseif ($clearance_flag) {
    $item_flag = "<font color='red' size='2'><em>Clearance!</em></font>";
}

if (trim($product_info_ary['pic']) == "" || !file_exists("attachments/" . $product_info_ary['pic'])) {
    $product_info_ary['pic'] = 'no_bigCN.gif';
}

//$width_inch = sprintf("%0.2f", $product_info_ary['width']/2.54);
//$height_inch = sprintf("%0.2f", $product_info_ary['height']/2.54);
//$length_inch = sprintf("%0.2f", $product_info_ary['length']/2.54);

$product_info_ary['width'] = sprintf("%0.2f", $product_info_ary['width']);
$product_info_ary['height'] = sprintf("%0.2f", $product_info_ary['height']);
$product_info_ary['length'] = sprintf("%0.2f", $product_info_ary['length']);

$width_inch = sprintf("%0.2f", $product_info_ary['width2']);
$height_inch = sprintf("%0.2f", $product_info_ary['height2']);
$length_inch = sprintf("%0.2f", $product_info_ary['length2']);

$product_info_ary['cbm'] = sprintf("%0.2f", $product_info_ary['cbm']);
$product_info_ary['cuft'] = sprintf("%0.2f", $product_info_ary['cuft']);

$remarks ="";
if(trim($product_info_ary['comment'])!='')
$remarks ="<br>" . $product_info_ary['commentzh'];
else
$remarks ="<br>" . $product_info_ary['comment'];
//$remarks = $product_info_ary['comment'] == '' ? '' : "<br>" . $product_info_ary['comment'];

if ($jansen_type==2) {
    $money_type = '美金';
    $product_info_ary['price_2'] = number_format($product_info_ary['price_2'], 2, '.', ',');
    $price_show = $product_info_ary['price_2'];
} elseif ($jansen_type == 1) {
    $money_type = '<span class="EnTextFamily">HKD</span>';
    $product_info_ary['price_3'] = number_format($product_info_ary['price_3'], 2, '.', ',');
    $price_show = $product_info_ary['price_3'] ;
}

//================   Ŀ¼Ϣʼ   ================//
$sql = "SELECT c.* FROM `product_category` pc,`category` c WHERE c.publish=1 AND c.id=pc.`category_id` AND pc.`product_id`='$pid' LIMIT 0,1";	
$cate_info_ary = $db->get_one($sql);
//if($categoryid=="" || $categoryid==0 || ($cate_info_ary["id"] > 0 && $categoryid != $cate_info_ary["id"]))
if($categoryid=="" || $categoryid==0 )
{
	$categoryid = $cate_info_ary["id"];
}
$vNull = null;
$listPage = 'product_photo_list.php';
if ($jansen_type > 0 && $jansen_uid > 0) 
{	
	$listPage = 'product_list.php';
}

$detailUrlParam = '';//URL,ԹRelated product洫

if($styleid >= 0 && $styleid!="")
{
	$listPage = $listPage.'?styleid=%d';
	$urlParam = 'styleid='.$styleid;
	if($styleid == 0) $styleid = $product_info_ary['style_id'];
	
	$category_list =  GetCategoryTable($_style,$listPage,0,$styleid,0,&$vNull);	
	
	$postiondiv = GetPositionLink($_style,$styleid,"style.php","风格/时期","styleid",&$positionName);
}
else if($settingid != ""  && $categoryid >= 0  && $categoryid != "")
{
	$listPage = $listPage.'?categoryid=%d&settingid=%d';
	$urlParam = 'settingid='.$settingid.'&categoryid='.$categoryid;
	if($settingid == 0) $settingid = $product_info_ary['setting_id'];
	$detailUrlParam = 'settingid='.$settingid.'&categoryid='.$categoryid. '&';
	$arySetting = GetSettingAry();
	$category_list = GetSettingMenu($arySetting,$listPage,$categoryid,$settingid);
	
	$postiondiv = GetSettingPositionLink($arySetting,$listPage,$categoryid,$settingid,&$positionName);
}
else if($collectionid >= 0 && $collectionid !="")
{
	$listPage = $listPage.'?collectionid=%d';
	$urlParam = 'collectionid='.$collectionid;
	if($collectionid == 0) $collectionid = $product_info_ary['collection_id'];
	$category_list =  GetCategoryTable($_collection,$listPage,0,$collectionid,0,&$vNull);
	
	$postiondiv = GetPositionLink($_collection,$collectionid,"collections.php","收藏品","collectionid",&$positionName);
}
else if($categoryid > 10000 && $settingid == "")//New Product
{
	$listPage = $listPage.'?categoryid=%d';
	$urlParam = 'categoryid='.$categoryid;
	
	$category_list = GetCategoryTable($_categories,$listPage,0,$categoryid,0,&$vNull);
	
    $postiondiv ='<div class="position" id="position"><a href="index.php">首页</a> &gt; <a href="products.php">产品</a> &gt; <a href="'.$listPage.'?categoryid=10001">新产品</a></div>';
}
else if($categoryid >= 0 && $categoryid <=10000 && $settingid == "" && $categoryid != "")	
{
	$listPage = $listPage.'?categoryid=%d';	
	$urlParam = 'categoryid='.$categoryid;
	
	$category_list = GetCategoryTable($_categories,$listPage,0,$categoryid,0,&$vNull);
	
	$postiondiv = GetProductCatePositionDiv($_categories,$categoryid,&$positionName);
}
else
{
	/*if($categoryid=="" ||$categoryid==0)
	{
		$sql = "SELECT c.* FROM `product_category` pc,`category` c WHERE c.id=pc.`category_id` AND pc.`product_id`='$pid' LIMIT 0,1";	
		$cate_info_ary = $db->get_one($sql);
		$categoryid = $cate_info_ary["id"];
		
		$listPage = $listPage.'?categoryid=%d';
		$urlParam = 'categoryid='.$categoryid;
		
		$category_list = GetCategoryTable($_categories,$listPage,0,$categoryid,0,&$vNull);
		
		$postiondiv = GetProductCatePositionDiv($_categories,$categoryid,&$positionName);
	}*/
}
//================   Ŀ¼Ϣ   ================//

//=============================== ƷϢ ==================================//

if($categoryid>0) {
	$strPage = $curpage != "" ? "&page=".$curpage : "";
    $return_href = "product_list.php?categoryid=".$categoryid.$strPage;
} 
else {
    $return_href = "javascript:history.back(1)";
}

$baner_img=GetBannerImg($positionName);

//Finishes array
$strDisplay ='style="display:none;"';
$strFirstFinishItem = '';
if($product_info_ary['finish_id'] > 0)
{
	$jsonFinishImagePath = 'FinishImagePath = {';
	$sql = 'select id,name,photo from p_finish_item where sid='.$product_info_ary['finish_id'].' order by name';
	$query = $db->query($sql);
	$strSep = '';
	$aryFinishItem = array();
	$i=0;
	while($rs=$db->fetch_array($query))
	{
		$jsonFinishImagePath .= $strSep. '"Finish_'.$rs["id"].'":';
		if(trim($rs["photo"])!='' && file_exists("attachments/".$rs["photo"]))
		{
			$rs["photo"] = 'attachments/'.$rs["photo"];
		}
		else
		{
			$rs["photo"] = '';
		}
		$jsonFinishImagePath.= '"'.$rs["photo"].'"';
		$aryFinishItem[] = $rs;
		if($i==0)
		{
			$strFirstFinishItem = $rs["photo"];
		}
		$strSep = ',';			
		$i++;
	}
	$jsonFinishImagePath.='};';
	
	if($i > 0 && trim($strFirstFinishItem !='')) $strDisplay ='';
	else $strDisplay = " style='display:none'";
}


//Setting product
$strSep = '';
$jsonSettingImage = '';
$arySettingProduct = array();
if($product_info_ary['setting_id'] > 0)
{
	$sql = 'select id,name,small_pic from product where setting_id='.$product_info_ary['setting_id'].' and hidden_flag=0 and id<>'.$pid;
	$query = $db->query($sql);
	while($rs=$db->fetch_array($query))
	{
		if($rs["small_pic"]=="" || !file_exists("attachments/".$rs["small_pic"]))
		{
			$rs["small_pic"] = 'attachments/noCN.gif';
		}
		else
		{
			$rs["small_pic"] = 'attachments/'.$rs["small_pic"];
		}	
		$arySettingProduct[] = $rs;
	}
	$jsonSettingImage = "jsonSettingImage = ".json_encode($arySettingProduct);
}

// Wish list 
$sql = "SELECT `id`, `name`,`namezh`,`date` FROM `wishlist` WHERE `user_id` = $jansen_uid ORDER BY `date` DESC";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
	if($rs["name"] == '') $rs["name"] = "Created on ".date('d/M/Y \a\t H:i', $rs['date']);
	$wishlist_ary[] = $rs;
}

require_once PrintEot('header3');

//if ((($categoryid == 0 || $categoryid == "") && ($fid == 0 || $fid == "") ) || $new) {
//    require_once PrintEot('product_details2');
//} else {
//    require_once PrintEot('product_details');
//}
require_once PrintEot('product_details');
require_once PrintEot('footer');

?>